Hi I have written a code to convert the files from excel to state and merge variables from two different files and lastly append the data sets. is there a way to make the code efficient please it is taking a lot of time
global temp_directory "C:\Users\ramzan\Downloads\Compressed\air_temp_201 4"
global precip_directory "C:\Users\ramzan\Downloads\Compressed\precip_2 017"
global years 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
******* Import the temp files into stata format
cd "$temp_directory"
foreach dataset in $years{
clear
import excel using air_temp_`dataset'.xlsx, sheet("Sheet1") firstrow
destring Year, replace
*export excel using air_temp_`var'.xlsx , firstrow(variables) replace
rename (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) (temp#), addnumber
reshape long temp, i(lat longitude Year) j(month)
label variable lat "Latitude"
generate date = ym(Year,month), after(month)
format %tm date
egen id_g = group (lat longitude)
*append using air_temp_
cd "$temp_directory"
save air_temp_`dataset'.dta , replace
}
******* Import the precip files into stata format
cd $precip_directory
foreach dataset in $years {
clear
import excel using precip_`dataset'.xlsx, sheet("Sheet1") firstrow
destring Year, replace
*export excel using air_temp_`var'.xlsx , firstrow(variables) replace
rename (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) (precip#), addnumber
reshape long precip, i(lat longitude Year) j(month)
label variable lat "Latitude"
generate date = ym(Year,month), after(month)
egen id_g = group (lat longitude)
format %tm date
*append using air_temp_
save precip_`dataset'.dta , replace
}
******* Merge temp and precip files
foreach dataset in $years {
cd $temp_directory
use air_temp_`dataset'.dta, clear
*rename Year year
cd $precip_directory
merge 1:1 id_g month Year using precip_`dataset'
drop _merge id_g
cd $temp_directory
save air_temp_`dataset'.dta, replace
}
******* Append temp and precip files
clear
use "air_temp_1981.dta", clear /* select first file*/
foreach dataset in 1982{
append using air_temp_`dataset' /*Combine datasets together*/
sort Year month ID
save air_temp_all.dta , replace
erase air_temp_`dataset'.dta /*Erase files we're done with*/
}
gen temp_c = (temp-32)*(5/9)
use air_temp_all.dta,clear
Comment